######################################################################
#Replication file for "The Impact of China's AIIB on the World Bank"
#Authors: Jing Qian, James Vreeland, Jianzhi Zhao
#Codes to replicate Figure A.2
######################################################################
rm(list = ls())

#To install the bpCausal package (ver 0.0.1)
# install.packages('devtools', repos = 'http://cran.us.r-project.org') # if not already installed
# devtools::install_github('liulch/bpCausal')

#Load bpCausal package
library(bpCausal)

#Other packages used
library(tidyverse)
library(RColorBrewer)

#Setwd
setwd("C:/Users/qianj/Dropbox (Princeton)/AIIB_WB_Replication")

#Load custom functions
#Note: Given the number of tests performed in the appendix, 
#several custom functions are created to implement estimation methods in a more efficient way.
#These functions merely create a wrap-up of existing functions, like bpCausal and gsynth,
#in order to avoid typing parameters repeatedly.

source("code/custom_functions.R")

###################################
#Estimation for Figure A.2
###################################
#----------------
#(1) Load data
#----------------
load("data/data_imputed.RData")
load("result/bpcausal_original.RData") #original results

#--------------
#(2) Setup
#--------------
D.use = "aiib_founder_2016"
dv.list = c("hard_project_count_all")
X.use = Z.use = A.use = c("gdppc_log_lag",
                          "population_log_lag",
                          "election_either_lag",
                          "fdi_gdp_lag",
                          "debt_gni_lag",
                          "oda_gni_lag",
                          "polity2_lag",
                          "unsc_lag",
                          "IdealPointDistance_lag")

#Container
result.figure.a2 = list()


#--------------
#(3) Estimation
#~25 minutes
#--------------
now = Sys.time()
for (i in 1:5){
  df.use = df.imputed[[i]]
  result.figure.a2[[i]] = bpcausal.group(dv.list = dv.list,
                                         df.use = df.use,
                                         D.use = D.use,
                                         X.use = X.use,
                                         Z.use = Z.use,
                                         A.use = A.use)
  print(i)
}

print(difftime(Sys.time(),
               now))

###################################
#Produce Figure A.2
###################################
#-------------
#(1) Prepare results
#-------------
#Summarize results
result.use = effSummary(result.original)
result.impute.use = lapply(result.figure.a2,
                           function(x) effSummary(x$hard_project_count_all))

#-------------
#(2) Parameters 
#-------------
#Parameters
n = 6
xlim = c(-2, 2)
cex = 2
xtext = 0.6
colors = brewer.pal(n = n, 
                    name = "Dark2")

#Result
att.all = result.use$est.avg
att.group = result.impute.use

#------------------
#(3) Plot
#------------------
#Setup
png(filename = "figure/Figure_A2.png",
    height = 800,
    width = 1200)

par(mar = c(5, 1, 5, 1))

#----------
#Empty plot
plot(1,
     type = "n",
     xlab = "Estimated Average Treatment Effect on the Treated",
     ylab = "",
     yaxt = "n",
     xaxt = "n",
     xlim = xlim,
     ylim = c(n + 3, 0),
     cex.lab = 2)
box()

#-------------
#Original
#-------------
points(x = att.all[1],
       y = 2,
       col = colors[1],
       pch = 19,
       cex = cex)
arrows(x0 = att.all[2],
       x1 = att.all[3],
       y0 = 2,
       y1 = 2,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[1],
       cex = cex,
       lwd = cex)

#----------------
#Imputed
#----------------
for (i in 1:(n-1)){
  points(x = att.group[[i]]$est.avg[1],
         y = 2 + i,
         col = colors[i+1],
         pch = 19,
         cex = cex)
  arrows(x0 = att.group[[i]]$est.avg[2],
         x1 = att.group[[i]]$est.avg[3],
         y0 = 2 + i,
         y1 = 2 + i,
         length = 0.05,
         angle = 90,
         code = 3,
         col = colors[i+1],
         cex = cex,
         lwd = cex)
}

#---------------
#Other elements
#Abline
abline(v = 0,
       lty = "dashed",
       lwd = cex,
       col = "grey")
#Axis
axis(side = 1,
     at = seq(min(xlim),
              max(xlim),
              1),
     cex.axis = cex)

#Title
title("World Bank Infrastructure Projects",
      cex.main = cex,
      line = 1)

#Legend
legend("topright",
       legend = c("Original",
                  paste0("Imputed ", 1:5)),
       cex = cex,
       col = colors,
       lty = rep(1, n),
       bty = "n",
       lwd = cex)

#-----------------
#Close
dev.off()